﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.ConfigForm.Children
{
    public partial class Security : Krile.Kernel.Control.ConfigPage
    {
        public Security()
        {
            InitializeComponent();
        }

        private void Security_Load(object sender, EventArgs e)
        {
            SaveErrorReport.Checked = Core.Config.Security.SaveErrorReport;
            IgnoreNetworkError.Checked = Core.Config.Security.IgnoreNetworkError;
            if (Core.Config.Security.Ignores != null)
                ErrorIgnoreData.Items.AddRange(Core.Config.Security.Ignores);
            StrictSecrity.Checked = Core.Config.Security.StrictSecurity;
            StrictSecrity_CheckedChanged(null, null);
        }

        private void StrictSecrity_CheckedChanged(object sender, EventArgs e)
        {
            loginkeyGroup.Enabled = StrictSecrity.Checked;
        }

        public override void Apply()
        {
            Core.Config.Security.SaveErrorReport = SaveErrorReport.Checked;
            Core.Config.Security.IgnoreNetworkError = IgnoreNetworkError.Checked;
            if (ErrorIgnoreData.Items.Count > 0)
            {
                List<Cores.Config._Security.ReportIgnoreData> nd = new List<Krile.Cores.Config._Security.ReportIgnoreData>();
                foreach (var i in ErrorIgnoreData.Items)
                {
                    nd.Add(i as Cores.Config._Security.ReportIgnoreData);
                }
                Core.Config.Security.Ignores = nd.ToArray();
            }
            else
            {
                Core.Config.Security.Ignores = null;
            }

            if (StrictSecrity.Checked)
            {
                if (String.IsNullOrEmpty(loginKey.Text) && String.IsNullOrEmpty(loginKeyReenter.Text))
                {
                    //no modified
                    if (!Core.Config.Security.StrictSecurity && String.IsNullOrEmpty(Core.Config.Security.KeyHash))
                    {
                        MessageBoxEx.Show(
                            null,
                            Lang.Msg.Config_LoginKeyInvalidState, Lang.Msg.Config_LoginKeyInvalidState_Caption,
                            MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Error);
                        return;
                    }
                    Core.Config.Security.StrictSecurity = true;
                }
                else
                {
                    //set
                    if (loginKey.Text != loginKeyReenter.Text)
                    {
                        MessageBoxEx.Show(Lang.Msg.Config_LoginKeyMismatch, Lang.Msg.Config_LoginKeyMismatch_Caption,
                            MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Error);
                        return;
                    }
                    Core.Config.Security.StrictSecurity = true;
                    byte[] checkBite = System.Text.Encoding.ASCII.GetBytes(loginKey.Text);
                    using (SHA512Managed sha = new SHA512Managed())
                    {
                        Core.Config.Security.KeyHash = BitConverter.ToString(sha.ComputeHash(checkBite));
                    }
                }
            }
            else
                Core.Config.Security.StrictSecurity = false;
        }

        private void ErrorIgnoreData_DoubleClick(object sender, EventArgs e)
        {
            if (ErrorIgnoreData.SelectedIndex >= 0)
            {
                ErrorIgnoreData.Items.RemoveAt(ErrorIgnoreData.SelectedIndex);
            }

        }
    }
}
